package com.mg.smarttask.client;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

public class ShowNotes extends Composite {
	VerticalPanel verticalPanel = new VerticalPanel();
	protected boolean shown = false;
	List<Note> notes= new ArrayList<Note>();
	private NotesWindow notesWindow;
	
	protected final Label emptyNote = new Label("Vous n'avez encore aucune note " +
			"et/ou aucune note n'est partag\u00E9e pour l'instant");
	private boolean rValue;
	public ShowNotes(NotesWindow notesWindow) {
		this.notesWindow = notesWindow;
		initWidget(verticalPanel);
	}	

	/**
	 * will set the value returned by editNote and DeleteNote
	 * @param rValue
	 */
	public void setReturnValue(boolean rValue){
		this.rValue = rValue;
	}
	
	/**
	 * Will reset The value returned by editNote and DleteNote and return true if
	 * the note was deleted or edited
	 * @return
	 */
	public boolean getReturnValue(){
		if(rValue){
			rValue = false;
			return true;
		}
	    return rValue;
	}
	public boolean editNote(Note note){
		NoteService.Util.getInstance().editNote(note, new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				GWT.log("Error", caught);
				Window.alert("Impossible de modifier");
			}

			@Override
			public void onSuccess(Boolean result) {
				setReturnValue(true);
			}
			
		});
		return getReturnValue();
	}
	
	
	/**
	 * Cette fonction supprime la note entr�e en parap�tre du serveur, et rafraichit l'affichage par apr�s
	 * @param note
	 */
	public void deleteNote(Note note){
		NoteService.Util.getInstance().deleteNote(note, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				GWT.log("Error", caught);
				Window.alert("Impossible de supprimer");
			}

			@Override
			public void onSuccess(Void result) {
				loadNotesFromServer();
			}
			
		});
	}
	/**
	 * Cette fonction acc�de aux notes contenus sur le serveur
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void loadNotesFromServer(){
		NoteService.Util.getInstance().getNotesList(1, new AsyncCallback<List>() {
					public void onFailure(Throwable caught) {						
						GWT.log("Error", caught);
					}

					public void onSuccess(List notesList) {
						setNotes(notesList);
					}
				});
	}
	
	public void setNotes(List<Note> notes){
		this.notes.clear();
		this.notes = (List<Note>)notes;
		updateNotePanel();
	}
	/**
	 * Mettre � jour les notes affich�s dans le panel,
	 *  et afficher empty si aucune note n'a pu �tre trouv�e
	 */
	void updateNotePanel(){
		verticalPanel.clear();
		if(this.shown) shown = false;
		if(notes.size()==0) {
			verticalPanel.add(emptyNote);
			return;
		}
	//	verticalPanel.add(new Label("Nombre de notes"+notes.size()));
		//panel contenant la vue unique et la note
		for(Note note : notes){
			addNoteToPanel(note);
		}
	}
	/**
	 * Devra ajouter un note au panel
	 * @param note est la note � ajouter au panel
	 */
	public void addNoteToPanel(Note note){
			NoteDisplay noteDisplay = new NoteDisplay(note);
			verticalPanel.add(noteDisplay);
	}
	
	private class NoteDisplay extends Composite{
		private VerticalPanel verticalPan = new VerticalPanel();
		private HorizontalPanel buttonPan = new HorizontalPanel();
		private HorizontalPanel buttonPanEdit = new HorizontalPanel();
		private Button delete = new Button();
		private Button share = new Button();
		private Button done = new Button();
		private Button edit = new Button();
		private Button cancelEdit = new Button();
		private Button validateEdit = new Button();

		@SuppressWarnings("deprecation")
		NoteDisplay(Note note){
			verticalPan.setStyleName("notePanel");
			final VerticalPanel rightContent = new VerticalPanel();
			rightContent.setStyleName("rightContent");
			buttonPan.setStyleName("buttonPan");
			
			
			final Note noote = note;
			initWidget(verticalPan);
			
			HorizontalPanel datePlusTitle = new HorizontalPanel();
			datePlusTitle.setStyleName("datePlusTitle");
			HTML dateCSS = new HTML(noote.getModificationDate().getDate() + " / " + noote.getModificationDate().getMonth() + " / " + (noote.getModificationDate().getYear() + 1900));
			dateCSS.setStyleName("dateCSS");
			final HTML noteCSS = new HTML(noote.getTitle());
			noteCSS.setStyleName("noteCSS");
			datePlusTitle.add(dateCSS);
			rightContent.add(noteCSS);
			
			
			final HTML description = new HTML(noote.getDescription());
			description.setStyleName("abc");
			
			rightContent.add(description);
			
			
			datePlusTitle.add(rightContent);
			
			verticalPan.add(datePlusTitle);
			

			buttonPan.add(done);
			done.setStyleName("btnDone");
			if(noote.getIsDone()) {
				done.addStyleName("undone");
			}
			else {
				done.addStyleName("done");
			}
			done.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {					
					if(!noote.getIsDone()){
						if(Window.confirm("Voulez vous signaler cette note comme faite?")){
							noote.setIsDone(true);
							editNote(noote);
							done.removeStyleName("done");
							done.addStyleName("undone");

						}
					} else {
						noote.setIsDone(false);
						editNote(noote);
						done.removeStyleName("undone");
						done.addStyleName("done");	
					}
				}
			});

			buttonPan.add(share); 
			share.setStyleName("btnShare");
			if(noote.getIsShared()) {
				share.addStyleName("unshare");
			}
			else {
				share.addStyleName("share");
			}
			share.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {					
					if(!noote.getIsShared()){
						if(Window.confirm("Voulez vous vraiment partager cette note?")){
							noote.setIsShared(true);
							editNote(noote);
								share.addStyleName("unshare");
								share.removeStyleName("share");														
						}
					} else {
						if(Window.confirm("Etes vous certain de vouloir mettre fin" +
								" au partage de cette note?")){
							noote.setIsShared(false);
							editNote(noote);
							share.addStyleName("share");							
								share.removeStyleName("unshare");
						}
					}
				}
			});

			
			
			buttonPan.add(edit);
			edit.setStyleName("btnEdit");
			edit.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					validateEdit.setStyleName("btnDone");
					cancelEdit.setStyleName("btnDelete");
					validateEdit.addStyleName("done");
					
					rightContent.clear();
					final TextBox noteTitle = new TextBox();
					noteTitle.setWidth("540px");
					final TextArea noteDescription = new TextArea();
					noteDescription.setWidth("540px");
					noteTitle.setText(noote.getTitle());
					noteDescription.setText(noote.getDescription());
							
					buttonPanEdit.add(validateEdit);
					validateEdit.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							noote.setTitle(noteTitle.getText());
							noote.setDescription(noteDescription.getText());
							editNote(noote);
							rightContent.clear();
							rightContent.add(noteCSS);
							rightContent.add(description);
							rightContent.add(buttonPan);
							updateNotePanel();
						}
					});
					
					buttonPanEdit.add(cancelEdit);
					cancelEdit.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							rightContent.clear();
							rightContent.add(noteCSS);
							rightContent.add(description);
							rightContent.add(buttonPan);
						}
					});
					
					rightContent.insert(noteTitle, 0);
					rightContent.insert(noteDescription, 1);
					rightContent.insert(buttonPanEdit, 2);
				}
			});
			buttonPan.add(delete); 
			delete.setStyleName("btnDelete");
			delete.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					if(Window.confirm("Voulez vous vraiment supprimer cette Note?")){
						deleteNote(noote);
						removeWidget();
					}
				}
			});
			
			
			rightContent.add(buttonPan);
		}
	    
		void removeWidget(){
			this.removeFromParent();
		}
	}
	
}
